﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;

namespace WcfService1
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        #region System function
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        #endregion

        #region My Services

        public User CheckAccount(string UserName, string Password)
        {
            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = context.SEBanking_Accounts
                        .Where(c => c.Acc_ID == UserName && c.Acc_Password == Password)
                        .Select(c => new { c.Acc_ID, c.Acc_Name, c.AccType_ID });
            if (query.Count() == 0)
                return null;
            else
            {
                User us = new User();
                foreach (var q in query)
                {
                    us.UserID = q.Acc_ID;
                    us.UserName = q.Acc_Name;
                    us.UserTypeID = q.AccType_ID;
                }

                return us;
            }
        }

        public decimal GetBalance(string idCard)
        {
            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = context.SEBanking_Cards.Single(id => id.Card_ID == idCard);
            return (decimal)query.Card_Banlance;
        }

        public int ExChangeVND(String idCardSource, String SecurityCode, String idCardTarget, decimal money)
        {
            if (money <= 0)
                return 0;
            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = from c in context.SEBanking_Cards
                        where c.Card_ID == idCardSource
                        select new { id = c.Card_ID, seritycode = c.Card_SecurityCode, balance = c.Card_Banlance };
            if (query.Count() == 0)
                return -1;
            else
            {
                foreach (var q in query)
                {
                    if (SecurityCode != q.seritycode)
                        return -3;
                    if ((decimal)q.balance < money)
                        return -2;

                    var cr = context.SEBanking_Cards.Single(l => l.Card_ID == idCardTarget);
                    if (cr == null)
                        return -4;

                    decimal b = (decimal)q.balance - money;

                    var cs = context.SEBanking_Cards.Single(c1 => c1.Card_ID == q.id);
                    cs.Card_Banlance = b;

                    cr.Card_Banlance += money;

                    SEBanking_Exchange ex = new SEBanking_Exchange();
                    ex.Card_ID_Send = idCardSource;
                    ex.Card_ID_Receive = idCardTarget;
                    ex.Exc_Balance = money;
                    context.SEBanking_Exchanges.InsertOnSubmit(ex);

                    context.SubmitChanges();
                }
                return 1;
            }
        }

        public int ExChangeUSD(String idCardSource, String SecurityCode, String idCardTarget, decimal money)
        {
            money *= 18000;
            return ExChangeVND(idCardSource, SecurityCode, idCardTarget, money);
        }

        public int Withdraw(String idCard, decimal money)
        {
            if (money <= 0)
                return 0;
            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = from c in context.SEBanking_Cards
                        where c.Card_ID == idCard
                        select new { id = c.Card_ID, balance = c.Card_Banlance };
            if (query.Count() == 0)
                return -1;
            else
                foreach (var q in query)
                {
                    if ((decimal)q.balance < money)
                        return -2;
                    decimal b = (decimal)q.balance - money;

                    var ca = context.SEBanking_Cards.Single(cr => cr.Card_ID == q.id);
                    ca.Card_Banlance = b;

                    SEBanking_Exchange ex = new SEBanking_Exchange();
                    ex.Card_ID_Send = idCard;
                    ex.Exc_Balance = money;
                    context.SEBanking_Exchanges.InsertOnSubmit(ex);

                    context.SubmitChanges();

                }
            return 1;
        }

        public int SendIn(String idCard, decimal money)
        {
            if (money <= 0)
                return 0;
            DataClassesSEBankingDataContext context = new DataClassesSEBankingDataContext();
            var query = from c in context.SEBanking_Cards
                        where c.Card_ID == idCard
                        select new { id = c.Card_ID, balance = c.Card_Banlance};
            if (query.Count() == 0)
                return -1;
            else
                foreach (var q in query)
                {
                    decimal b = money + (decimal)q.balance;
                    var ca = context.SEBanking_Cards.Single(cr => cr.Card_ID == q.id);
                    ca.Card_Banlance = b;

                    SEBanking_Exchange ex = new SEBanking_Exchange();
                    ex.Card_ID_Receive = idCard;
                    ex.Exc_Balance = money;
                    context.SEBanking_Exchanges.InsertOnSubmit(ex);

                    context.SubmitChanges();

                }
            return 1;                        
        }

        #endregion
    }
}
